package org.xm.sk.vo.tb;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.xm.sk.vo.Base;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
@Table(name = "dc_shop")
@ApiModel(value = "Shop", parent = Base.class, description = "门店")
public class Shop extends Base {

    /**
     * 
     */
    private static final long serialVersionUID = 1555333441083285496L;

    @ApiModelProperty(value = "门店名称")
    @Column(length = 255)
    private String name;

    @ApiModelProperty(value = "门店所属品牌")
    @JsonIgnore
    @ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinColumn(name = "dc_brand_id")
    private Brand brand;

    @ApiModelProperty(value = "门店营业时间")
    @Column(length = 255)
    private String businessHours;

    @ApiModelProperty(value = "门店联系电话")
    @Column(length = 20)
    private String phone;

    @ApiModelProperty(value = "门店地址")
    @Column(length = 255)
    private String address;

    @ApiModelProperty(value = "门店坐标位置-纬度")
    @Column
    private Double latitude;
    
    @ApiModelProperty(value = "门店坐标位置-经度")
    @Column
    private Double longitude;

    @ApiModelProperty(value = "门店满意度")
    @Column(length = 10)
    private String starlevel;

    @ApiModelProperty(value = "门店展示图片")
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "dc_shop_image", joinColumns = {
            @JoinColumn(name = "dc_shop_id") }, inverseJoinColumns = {
                    @JoinColumn(name = "dc_image_id") })
    @Fetch(FetchMode.SUBSELECT)
    private Set<Image> images = new HashSet<Image>(0);

    @ApiModelProperty(value = "门店下所有发型师")
    @JsonIgnore
    @OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "shop")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Hairdresser> hairdressers = new HashSet<Hairdresser>(0);

    @ApiModelProperty(value = "门店拥有的优惠券")
    @JsonIgnore
    @OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "shop")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Coupon> coupons = new HashSet<Coupon>(0);

    @ApiModelProperty(value = "对门店的评论")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "shops")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Mete> metes = new HashSet<Mete>(0);

    @ApiModelProperty(value = "门店管理员")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "shops")
    @Fetch(FetchMode.SUBSELECT)
    private Set<User> users = new HashSet<User>(0);

    @ApiModelProperty(value = "关注此门店的粉丝")
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinTable(name = "dc_shop_fans", joinColumns = {
            @JoinColumn(name = "dc_shop_id") }, inverseJoinColumns = {
                    @JoinColumn(name = "dc_user_id") })
    @Fetch(FetchMode.SUBSELECT)
    private Set<User> fans = new HashSet<User>(0);
    
    @ApiModelProperty(value = "门店适用的优惠活动")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "shops")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Activity> activitys = new HashSet<Activity>(0);
    
    @ApiModelProperty(value = "门店与用户之间的距离")
    @Transient
    private Double distance;
    
    @ApiModelProperty(value = "品牌ID")
    @Transient
    private Integer brandId;
    
    @ApiModelProperty(value = "品牌名称")
    @Transient
    private String brandName;
    
    @ApiModelProperty(value = "门店宣传图片ID集")
    @Transient
    private Set<Integer> imageIds = new HashSet<Integer>(0);
    
    @ApiModelProperty(value = "粉丝数")
    @Transient
    private Long fansCount;
    
    public void setDistance(Double distance) {
        this.distance = Double.parseDouble(String.format("%.2f", distance));
    }

}
